package cn.work.control;
import cn.work.bean.reservationBean;
import cn.work.bean.userbean;
import cn.work.service.ReservationService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;

@WebServlet(name = "ReservationServlet", value = "/ReservationServlet")
public class ReservationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        ReservationService reservationService = new ReservationService();
        String action = request.getParameter("action");
        if (action == null || action.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("缺少必要的参数: action");
            return;  // 立即返回，避免继续处理
        }

        if(action.equals("add")){
            HttpSession session = request.getSession();
            userbean user = (userbean) session.getAttribute("user");
            reservationBean reservation = new reservationBean();
            reservation.setUser_id(user.getUid());
            if(user.getIdentity()==1){
                reservation.setUser_id(null);
                reservation.setRemark("");
            }
            else{
                reservation.setRemark(request.getParameter("remark"));
            }
            reservation.setFullname(request.getParameter("name"));
            reservation.setUser_tel(request.getParameter("tel"));
            reservation.setService_id(Integer.parseInt(request.getParameter("service_id")));
            reservation.setBeau_id(Integer.parseInt(request.getParameter("beau_id")));
            reservation.setService_date(request.getParameter("dateSelect"));
            reservation.setService_period(request.getParameter("period"));
            boolean success;
            try {
                success = reservationService.processReservationAction(reservation,action);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            if (success) {
                response.setStatus(HttpServletResponse.SC_OK);
                response.getWriter().write("操作成功");
            } else {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                response.getWriter().write("操作失败");
            }
        }
        else {
            String reservationId = request.getParameter("id");
            if (reservationId == null) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().write("缺少必要的参数");
                return;
            }
            try {
                int id = Integer.parseInt(reservationId);
                boolean success = reservationService.processReservationAction(id, action);

                if (success) {
                    response.setStatus(HttpServletResponse.SC_OK);
                    response.getWriter().write("操作成功");
                } else {
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    response.getWriter().write("操作失败");
                }

            } catch (NumberFormatException e) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().write("无效的预约ID");
            } catch (SQLException e) {
                // 如果是数据库操作错误
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                response.getWriter().write("数据库错误: " + e.getMessage());
            } catch (Exception e) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                response.getWriter().write("服务器内部错误: " + e.getMessage());
                e.printStackTrace(); // 打印详细错误堆栈
            }
        }
    }
}

